#include "SHT20.h"
#include "main.h" 	  

void sht20_test_task(void *pvParameter);
#if 1
SHT2x_data SHT20;
u8 SHT2x_Init(void)
{
	u8 err;
	//IIC_Init();
	err = SHT2x_SoftReset();
    err = SHT2x_SoftReset();
	if(err==1)
		printf("SHT20初始化失败!\r\n");
	else
    {

        printf("SHT20初始化成功!\r\n");
        //xTaskCreate(sht20_test_task,"sht20_test_task",1024*2,NULL,2,NULL);
    }
		
	


	return err;
}

void sht20_test_task(void *pvParameter)
{
	uint8_t vol=0;
	while (1) 
    {

        sht20_DEBUG("温度:%d\r\n",SHT2x_GetTempPoll());
        sht20_DEBUG("湿度:%d\r\n",SHT2x_GetHumiPoll());

		vTaskDelay(1000 / portTICK_PERIOD_MS); 			 
 	}
	vTaskDelete(NULL);
}


u8 SHT2x_SoftReset(void) //SHT20软件复位
{
	u8 err=0;
	IIC_Start();
	IIC_Send_Byte(0x80);
	err = IIC_Wait_Ack();
	IIC_Send_Byte(0xFE);
	err = IIC_Wait_Ack();
	IIC_Stop();
	return err;
}
int SHT2x_GetTempPoll(void)
{
    float TEMP;
    u8 ack, tmp1, tmp2;
    u16 ST;
    u16 i=0;
    IIC_Start();				//发送IIC开始信号
    IIC_Send_Byte(I2C_ADR_W);			//IIC发送一个字节 
	ack = IIC_Wait_Ack();	
	IIC_Send_Byte(TRIG_TEMP_MEASUREMENT_POLL);
	ack = IIC_Wait_Ack();
    do 
	{
		delay_ms(10);               
		IIC_Start();				//发送IIC开始信号
		IIC_Send_Byte(I2C_ADR_R);	
		i++;
		ack = IIC_Wait_Ack();
		if(i==10)break;
    } while(ack!=0);


    tmp1 = IIC_Read_Byte(1);
    tmp2 = IIC_Read_Byte(1);
	IIC_Read_Byte(0);
    IIC_Stop();
    ST = (tmp1 << 8) | (tmp2 << 0);
    ST &= ~0x0003;
    TEMP = ((float)ST * 0.00268127) - 46.85;
    return (TEMP*100);	  
}

int SHT2x_GetHumiPoll(void)
{
    float HUMI;
    u8 ack, tmp1, tmp2;
    u16 SRH;
	u16 i=0;
    
    IIC_Start();				//发送IIC开始信号
    IIC_Send_Byte(I2C_ADR_W);			//IIC发送一个字节 
	ack = IIC_Wait_Ack();	
	IIC_Send_Byte(TRIG_HUMI_MEASUREMENT_POLL);
	ack = IIC_Wait_Ack();    
    do 
	{
		delay_ms(10);               
		IIC_Start();				//发送IIC开始信号
		IIC_Send_Byte(I2C_ADR_R);	
		i++;
		ack = IIC_Wait_Ack();
		if(i==10)break;
    } while(ack!=0);
    
    tmp1 = IIC_Read_Byte(1);
    tmp2 = IIC_Read_Byte(1);
    IIC_Read_Byte(0);
    IIC_Stop();
    
    SRH = (tmp1 << 8) | (tmp2 << 0);
    SRH &= ~0x0003;
    HUMI = ((float)SRH * 0.00190735) - 6;

    return (HUMI*100);
}
#else

SHT2x_data SHT20;
uint8_t SHT2x_Init(void)
{
	uint8_t err;
    i2c_init();

	err = SHT2x_SoftReset();
	if(err!=ESP_OK)
		printf("SHT20错误!!!\r\n");
	else
		printf("SHT20就绪!!!\r\n");
	
	return err;
}


u8 SHT2x_SoftReset(void) //SHT20软件复位
{
	esp_err_t ret =0;

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();                                   //新建操作I2C句柄
    i2c_master_start(cmd);                                                          //启动I2C
    i2c_master_write_byte(cmd, ( SHT20_ADR << 1 ) | WRITE_BIT, ACK_CHECK_EN);    //发地址+写+检查ack
    ret = i2c_master_write_byte(cmd, SOFT_RESET, ACK_CHECK_EN);    //发地址+写+检查ack
    i2c_master_stop(cmd);                                                           //停止I2C                                                        
    ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);        //I2C发送   
    i2c_cmd_link_delete(cmd);     //删除I2C句柄
    return ret;
}


int SHT2x_GetTempPoll(void)
{
    float TEMP;
    uint8_t ack, tmp1, tmp2,tmp3;
    uint16_t ST;
    uint16_t i=0;
    esp_err_t ret=0;

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();                                   //新建操作I2C句柄
    i2c_master_start(cmd);                                                          //启动I2C
    i2c_master_write_byte(cmd, ( SHT20_ADR << 1 ) | WRITE_BIT, ACK_CHECK_EN); 
    i2c_master_write_byte(cmd, TRIG_TEMP_MEASUREMENT_POLL, ACK_CHECK_EN); 
    i2c_master_start(cmd);
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, ( SHT20_ADR << 1 ) | READ_BIT, ACK_CHECK_EN);       
    i2c_master_read_byte(cmd, &tmp1, ACK_VAL); 
    i2c_master_read_byte(cmd, &tmp2, ACK_VAL); 
    i2c_master_stop(cmd);                                         //停止I2C
    ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);        //I2C发送 
    i2c_cmd_link_delete(cmd);     //删除I2C句柄
    if (ret != ESP_OK) {
        printf("SHT20错误\r\n");
        return ret;
    }   


    ST = (tmp1 << 8) | (tmp2 << 0);
    ST &= ~0x0003;
    TEMP = ((float)ST * 0.00268127) - 46.85;
    return (uint16_t)(TEMP*100);	  
}
/*
uint16_t SHT2x_GetHumiPoll(void)
{
    float HUMI;
    uint8_t ack, tmp1, tmp2;
    uint16_t SRH;
	uint16_t i=0;
    
    IIC_Start();				//发送IIC开始信号
    IIC_Send_Byte(I2C_ADR_W);			//IIC发送一个字节 
	ack = IIC_Wait_Ack();	
	IIC_Send_Byte(TRIG_HUMI_MEASUREMENT_POLL);
	ack = IIC_Wait_Ack();    
    do 
	{
		delay_ms(100);               
		IIC_Start();				//发送IIC开始信号
		IIC_Send_Byte(I2C_ADR_R);	
		i++;
		ack = IIC_Wait_Ack();
		if(i==100)break;
    } while(ack!=0);
    
    tmp1 = IIC_Read_Byte(1);
   
    tmp2 = IIC_Read_Byte(1);
    IIC_Read_Byte(0);
    IIC_Stop();
    
    SRH = (tmp1 << 8) | (tmp2 << 0);
    SRH &= ~0x0003;
    HUMI = ((float)SRH * 0.00190735) - 6;

    return (HUMI*100);
}*/

#endif